/*!
    \copyright  Copyright (c) 2022 - 2024 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file
    \ingroup    tx_power
    \brief
*/

#include "tx_power_advertising.h"

#include "le_advertising_manager.h"
#include "tx_power.h"
#include <logging.h>

#define TX_POWER_ADV_SIZE    (3)

static le_adv_item_handle tx_power_registered_handle = NULL;
static le_adv_item_data_t tx_power_advert;
static uint8 tx_power_adv_data[TX_POWER_ADV_SIZE];

static bool txPower_GetItemData(le_adv_item_data_t * item)
{
    DEBUG_LOG("txPower_GetAdvertisingDataItem tx_power_advert.data:%d", tx_power_advert.data);

    tx_power_advert.size = TX_POWER_ADV_SIZE;

    /* Set the data field in the format of advertising packet format. Adhering to le_advertising_mgr */
    tx_power_adv_data[0] = TX_POWER_ADV_SIZE - 1 ;
    tx_power_adv_data[1] = (uint8)ble_ad_type_tx_power_level;
    tx_power_adv_data[2] = TxPower_LEGetData();
    tx_power_advert.data = tx_power_adv_data;

    *item = tx_power_advert;
    return TRUE;
}

static bool txPower_GetItemInfo(le_adv_item_info_t * info)
{
    PanicNull(info);
    *info = (le_adv_item_info_t){ 
#ifdef PREFER_LEGACY_ADVERTISING
                                    .placement = le_adv_item_data_placement_advert,
                                    .type = le_adv_type_legacy_connectable_scannable,
#else
                                    .placement = le_adv_item_data_placement_advert,
                                    .type = le_adv_type_extended_connectable,
#endif
                                    .data_size = TX_POWER_ADV_SIZE };
    return TRUE;
}

le_adv_item_callback_t txPower_AdvertisingManagerCallback =
{
    .GetItemData = &txPower_GetItemData,
    .GetItemInfo = &txPower_GetItemInfo
};

bool TxPower_SetupLeAdvertisingData(void)
{
    tx_power_registered_handle = LeAdvertisingManager_RegisterAdvertisingItemCallback(NULL, &txPower_AdvertisingManagerCallback);
    return (tx_power_registered_handle ? TRUE : FALSE);
}

bool TxPower_UpdateLeAdvertisingData(void)
{
    return (tx_power_registered_handle ? LeAdvertisingManager_UpdateAdvertisingItem(tx_power_registered_handle) : FALSE);
}
